package servicesImp.dictionary;

import base.IBaseMapperWithoutBLOBs;
import base.IBaseServicesWithoutBLOBsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import wt.dao.dictionary.ItemTypeMapper;
import wt.entity.dictionary.ItemType;
import wt.entity.dictionary.ItemTypeExample;
import wt.entity.dictionary.MajorType;
import wt.staticData.TableName;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class ItemTypeServicesImp extends IBaseServicesWithoutBLOBsImpl<ItemType, ItemTypeExample> implements ItemTypeServices {
    @Autowired
    ItemTypeMapper itemTypeMapper;
    @Autowired
    MajorTypeServices majorTypeServices;

    @Override
    public IBaseMapperWithoutBLOBs<ItemType, ItemTypeExample> getMapper() {
        return this.itemTypeMapper;
    }

    @Override
    public String getTableName() {
        return TableName.DIC_ITEM_TYPE;
    }

    public List<ItemType> selectLiveItemType() throws Exception {
        ItemTypeExample itemTypeExample = new ItemTypeExample();
        itemTypeExample.createCriteria().andLiveEqualTo((short) 1);
        return selectByExample(itemTypeExample);
    }


    @Transactional
    public void insertSelectiveAutoInsertMajorType(ItemType itemType) throws Exception {
        insertSelective(itemType);
        MajorType majorType = new MajorType();
        majorType.setItemId(itemType.getId());
        majorType.setCurrentDepth(1);
        majorType.setMaxDepth(itemType.getSeries());
        majorType.setLive((short) 1);
        majorType.setName(itemType.getName());
        majorType.setNum(itemType.getNum());
        majorType.setOrders(new Date().getTime());
        majorTypeServices.insertSelective(majorType);
    }

    public ItemType selectByItemNum(String itemNum) throws Exception {
        ItemTypeExample itemTypeExample = new ItemTypeExample();
        itemTypeExample.createCriteria().andNumEqualTo(itemNum);
        List<ItemType> itemTypeList = selectByExample(itemTypeExample);
        if (itemTypeList.isEmpty()) {
            return null;
        } else {
            return itemTypeList.get(0);
        }
    }
}
